﻿//Copyright (C) <2013>  <jonathan cleeve norton> All Rights Reserved 
//Contact jon.norton@fin-plus.co.uk website <http://www.fin-plus.co.uk/>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using FinPlusCommon;
using log = Logger.Logger;

namespace FinPlusWPFControls
{
    public partial class FinPlusColButtonBar : FinPlusControl
    {
        public override string Id { get; set; }
        public override string[] States { get { return new string[] {}; } }
        public override string[] TestStates { get { return new string[] { "IsValid" }; } }
        public override IFinPlusFunctions Model { get { return null; } }
        public override string ModelTest { get { return null; } }
        public override bool IsValid { get; set; }

        public event EventHandler<Function> ColButtonClick;

        private enum Params { ColButton, Action, Publish }

        //construct
        public FinPlusColButtonBar(string buttons)
        {
            InitializeComponent();

            foreach (var button in buttons.Split(';').ToList<string>())
                AddButton(button);

            IsValid = true;
        }

        //common control interface
        public override void Dock(object control, string dock)
        {
            IsValid = false;
            throw new NotImplementedException();
        }

        public override void Initialise()
        {

        }

        public override void CommandRouter(IFinPlusFunction func)
        {
            try
            {
                CaptureCommand(func);

                switch ((Commands)Enum.Parse(typeof(Commands), func.Name, true))
                {
                    case Commands.ColButton: ColButtonClick.Invoke(this, (Function)func); break;
                    default: throw new Exception(string.Format("event not recognised {0}", func.Name));
                }
            }
            catch (Exception e)
            {
                IsValid = log.Log(Logger.Level.Warning, e.Message, e);
            }
        }

        public override void Dispose(){}
    
        //private
        private void AddButton(string value)
        {
            var b = value.Split(',');
            var button = new Button() { Content = b[0], Tag = value };
            button.Style = (Style)this.FindResource("DarkButtonStyle");
            button.Click += Button_Click;
            buttonBar.Children.Add(button);
        }

        //cmds
        private new enum Commands { ColButton }

        //events
        private void Button_Click(object sender, RoutedEventArgs a) { CommandRouter(new Function(Commands.ColButton.ToString(), Id, User, -1, ((Button)sender).Tag.ToString())); }
    }
}
